#if !defined(__APP_USART_DMA__)
#define __APP_USART_DMA__
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "stdio.h"
#include "string.h"
#include "queue.h"
#include <stdarg.h>
#include "hc32_ll_tmr6.h"
#include <stdbool.h>
#include "app_protocol.h"
#include "semphr.h"
#include "app_gpio.h"
#include "nczallcode.h"
/// @brief 创建队列
typedef struct uart_handle
{
    QueueHandle_t uartqueue;
    void (*send_data)(uint8_t *data, uint16_t length);
} UartHandle_t;

extern UartHandle_t uart_handle;

/* DMA definition122 */
#define RX_DMA_UNIT (CM_DMA1)
#define RX_DMA_CH (DMA_CH0)
#define RX_DMA_FCG_ENABLE() (FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_DMA1, ENABLE))
#define RX_DMA_TRIG_SEL (AOS_DMA1_0)
#define RX_DMA_TRIG_EVT_SRC (EVT_SRC_USART1_RI)
#define RX_DMA_RECONF_TRIG_SEL (AOS_DMA_RC)
#define RX_DMA_RECONF_TRIG_EVT_SRC (EVT_SRC_AOS_STRG)
#define RX_DMA_TC_INT (DMA_INT_TC_CH0)
#define RX_DMA_TC_FLAG (DMA_FLAG_TC_CH0)
#define RX_DMA_TC_IRQn (INT000_IRQn)
#define RX_DMA_TC_INT_SRC (INT_SRC_DMA1_TC0)

#define TX_DMA_UNIT (CM_DMA2)
#define TX_DMA_CH (DMA_CH0)
#define TX_DMA_FCG_ENABLE() (FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_DMA2, ENABLE))
#define TX_DMA_TRIG_SEL (AOS_DMA2_0)
#define TX_DMA_TRIG_EVT_SRC (EVT_SRC_USART1_TI)
#define TX_DMA_TC_INT (DMA_INT_TC_CH0)
#define TX_DMA_TC_FLAG (DMA_FLAG_TC_CH0)
#define TX_DMA_TC_IRQn (INT001_IRQn)
#define TX_DMA_TC_INT_SRC (INT_SRC_DMA2_TC0)

/* Timer0 unit & channel definition */
#define TMR0_UNIT (CM_TMR0_1)
#define TMR0_CH (TMR0_CH_A)
#define TMR0_FCG_ENABLE() (FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_1, ENABLE))


////OTA相关
#define TMR0_TRIG_CH                    (AOS_TMR0)
#define TMR0_CH_INT                     (TMR0_INT_CMP_B)
#define TMR0_CH_FLAG                    (TMR0_FLAG_CMP_B)
#define TMR0_INT_SRC                    (INT_SRC_TMR0_1_CMP_B)
#define TMR0_IRQn                       (INT006_IRQn)

#define QUENEDATAMAXLEN  30  //uart rx max len

/* USART RX/TX pin definition */
#define USART_RX_PORT (GPIO_PORT_A) /* PH13: USART1_RX */
#define USART_RX_PIN (GPIO_PIN_10)
#define USART_RX_GPIO_FUNC (GPIO_FUNC_20)


//RX2
#define USART_RX2_PIN (GPIO_PIN_03)


#define USART_TX_PORT (GPIO_PORT_A) /* PH15: USART1_TX */
#define USART_TX_PIN (GPIO_PIN_09)
#define USART_TX_GPIO_FUNC (GPIO_FUNC_20)

//TX2
#define USART_TX2_PIN (GPIO_PIN_02)

/* USART unit definition */
#define USART_UNIT (CM_USART1)
#define USART_UNIT2 (CM_USART2)
#define USART_FCG_ENABLE() (FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_USART1, ENABLE))

/* USART baudrate definition */
#define USART_BAUDRATE (115200UL)

/* USART timeout bits definition */
#define USART_TIMEOUT_BITS (2000U)

/* USART interrupt definition */
#define USART_TX_CPLT_IRQn (INT002_IRQn)
#define USART_TX_CPLT_INT_SRC (INT_SRC_USART1_TCI)
#define USART2_TX_CPLT_INT_SRC (INT_SRC_USART2_TCI)

#define USART_RX_ERR_IRQn (INT003_IRQn)
#define USART_RX_ERR_INT_SRC (INT_SRC_USART1_EI)
#define USART2_RX_ERR_INT_SRC (INT_SRC_USART2_EI)

#define USART_RX_TIMEOUT_IRQn (INT004_IRQn)
#define USART_RX_TIMEOUT_INT_SRC (INT_SRC_USART1_RTO)
#define USART2_RX_TIMEOUT_INT_SRC (INT_SRC_USART2_RTO)

/* Application frame length max definition */
#define APP_FRAME_LEN_MAX (27U)
#define APP_TX_LEN_MAX (1500U)

#define USART_CH                        (M4_USART1)
#define TIMER6_OVF_INT_NUM               (INT_SRC_TMR6_1_OVF)
#define TIMER6_OVF_INT_IRQn              (INT002_IRQn)

/*DMA通道初始传输计数设置为128 */
#define DMA_RX_INIT_COUNT  128U


//环形缓存大小
#define BUFFER_SIZE 14

/* 定义接收数据队列（用于传递接收到的缓冲区指针和长度） */
#define RX_QUEUE_LENGTH 5
#define RX_BUFFER_SIZE APP_FRAME_LEN_MAX

typedef struct {
    uint8_t *pData;
    uint16_t u16Len;
} RxDataMsg;

extern QueueHandle_t xRxQueue;                // 接收数据队列
extern SemaphoreHandle_t xRxBufferMutex;      // 缓冲区互斥锁





// 1. 定义数据结构体
typedef struct {
    uint16_t length;
    uint8_t data[APP_FRAME_LEN_MAX];
} UartRxData;


//flash读取存入数据
uint8_t Flash_Init(void);
uint8_t Flash_WriteU8(uint8_t index, uint8_t data);
uint8_t Flash_ReadU8(uint8_t index, uint8_t *data);

//提供接口给实时数据 获取
uint8_t get_out_bed_parameter(void);
uint8_t get_out_bed_warn(void);
uint8_t get_sit_up_warn(void);

void uart_process_task(void *pvParameters);

void get_passbackflash_task(void *pvParameters);

//串口一初始化
void App_uart_init(void);

//串口二初始化
void App_uart2_init(void);

//串口二发送数据
void USART2_SendData(CM_USART_TypeDef *USARTx,uint8_t *data, uint16_t length);

int32_t DMA_Config(void);

uint8_t get_otaflag(void);

void set_otaflag(uint8_t flag);

void TMR0_Config(uint16_t u16TimeoutBits);

void USART_SendData_DMA(uint8_t *pData, uint16_t len);



#endif // __APP_USART_DMA__


